{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "71ce86fa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Pregnancies</th>\n",
       "      <th>Glucose</th>\n",
       "      <th>BloodPressure</th>\n",
       "      <th>SkinThickness</th>\n",
       "      <th>Insulin</th>\n",
       "      <th>BMI</th>\n",
       "      <th>DiabetesPedigreeFunction</th>\n",
       "      <th>Age</th>\n",
       "      <th>Outcome</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>6</td>\n",
       "      <td>148</td>\n",
       "      <td>72</td>\n",
       "      <td>35</td>\n",
       "      <td>0</td>\n",
       "      <td>33.6</td>\n",
       "      <td>0.627</td>\n",
       "      <td>50</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>85</td>\n",
       "      <td>66</td>\n",
       "      <td>29</td>\n",
       "      <td>0</td>\n",
       "      <td>26.6</td>\n",
       "      <td>0.351</td>\n",
       "      <td>31</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>8</td>\n",
       "      <td>183</td>\n",
       "      <td>64</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>23.3</td>\n",
       "      <td>0.672</td>\n",
       "      <td>32</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>89</td>\n",
       "      <td>66</td>\n",
       "      <td>23</td>\n",
       "      <td>94</td>\n",
       "      <td>28.1</td>\n",
       "      <td>0.167</td>\n",
       "      <td>21</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>137</td>\n",
       "      <td>40</td>\n",
       "      <td>35</td>\n",
       "      <td>168</td>\n",
       "      <td>43.1</td>\n",
       "      <td>2.288</td>\n",
       "      <td>33</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Pregnancies  Glucose  BloodPressure  SkinThickness  Insulin   BMI  \\\n",
       "0            6      148             72             35        0  33.6   \n",
       "1            1       85             66             29        0  26.6   \n",
       "2            8      183             64              0        0  23.3   \n",
       "3            1       89             66             23       94  28.1   \n",
       "4            0      137             40             35      168  43.1   \n",
       "\n",
       "   DiabetesPedigreeFunction  Age  Outcome  \n",
       "0                     0.627   50        1  \n",
       "1                     0.351   31        0  \n",
       "2                     0.672   32        1  \n",
       "3                     0.167   21        0  \n",
       "4                     2.288   33        1  "
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "# Python中，'r'前缀用于处理原始字符串，避免反斜杠被解释成转义字符。\n",
    "# 使用原始字符串可以在处理正则表达式、文件路径、JSON字符串等情况下更方便，避免混淆和转义问题\n",
    "df = pd.read_csv(r'./data/pima-indians-diabetes.data',sep=',')\n",
    "df.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f1c125a0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(768, 9)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1a46b2c4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 768 entries, 0 to 767\n",
      "Data columns (total 9 columns):\n",
      " #   Column                    Non-Null Count  Dtype  \n",
      "---  ------                    --------------  -----  \n",
      " 0   Pregnancies               768 non-null    int64  \n",
      " 1   Glucose                   768 non-null    int64  \n",
      " 2   BloodPressure             768 non-null    int64  \n",
      " 3   SkinThickness             768 non-null    int64  \n",
      " 4   Insulin                   768 non-null    int64  \n",
      " 5   BMI                       768 non-null    float64\n",
      " 6   DiabetesPedigreeFunction  768 non-null    float64\n",
      " 7   Age                       768 non-null    int64  \n",
      " 8   Outcome                   768 non-null    int64  \n",
      "dtypes: float64(2), int64(7)\n",
      "memory usage: 54.1 KB\n"
     ]
    }
   ],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "569c70ad",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 查看数据集目标特征的分布\n",
    "df['Outcome'].value_counts() \n",
    "# 按数据集按照输入特征和目标特征进行划分，分布命名为X，y,且保证其类型为numpy.ndarrav\n",
    "from sklearn.model_selection import train_test_split\n",
    "from collections import Counter\n",
    "x_cols = [col for col in df.columns if col!='Outcome']\n",
    "y_col = 'Outcome'\n",
    "X=df[x_cols].values #dataframe 转化为ndarray,才能进入下面的标准化\n",
    "y=df[y_col].values\n",
    "#选择不过滤和降维的全部数据进行训练，\n",
    "#将原始数据框按输入特征和目标特征划分后，再划分为训练集和测试集"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff13bc62",
   "metadata": {},
   "source": [
    "1.划分数据集X，y，分别命名为X_train,X_test,y_train,y_test，测试集比例为10%，固定随机数种子为42，打乱顺序，并以df[y_col]做分层抽样。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f19ee0c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "#由考生填写\n",
    "X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.1,random_state=42,stratify=df[y_col])\n",
    "#由考生填写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "03ac12e4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Distribution of y train Counter({0: 450, 1: 241})\n",
      "Distribution of y test Counter({0: 50, 1: 27})\n"
     ]
    }
   ],
   "source": [
    "# 查看划分后的训练集和测试集中两类目标值的数量\n",
    "print('Distribution of y train {}'.format(Counter(y_train)))\n",
    "print('Distribution of y test {}'.format(Counter(y_test)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "36a7a317",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 引入StandScaler标准化工具库\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "std_scaler = StandardScaler().fit(X)\n",
    "X_train = std_scaler.transform(X_train) #请注意此处名称\n",
    "X_test = std_scaler.transform(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "eb5c1da4",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.naive_bayes import GaussianNB\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.ensemble import GradientBoostingClassifier\n",
    "models = []\n",
    "models.append(('KNN',KNeighborsClassifier()))\n",
    "models.append(('SVC',SVC()))\n",
    "models.append(('LR',LogisticRegression()))\n",
    "models.append(('DT',DecisionTreeClassifier()))\n",
    "models.append(('GNB',GaussianNB()))\n",
    "models.append(('RF',RandomForestClassifier()))\n",
    "models.append(('GB',GradientBoostingClassifier()))\n",
    "from sklearn.model_selection import cross_validate\n",
    "from sklearn.metrics import accuracy_score\n",
    "names = []\n",
    "scores = []"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dffb93a3",
   "metadata": {},
   "source": [
    "2.遍历models中的每一个模型，分别使用训练集(x_train,y_train)进行训练，然后用测试集x_test进行预测，计算准确率accuracy_score，并将模型名称name和准确率accuracy_score分别存储在列表names和scores中，最后将列表names和scores转换成DataFrame格式，DataFrame命名为tr_split，tr_split打印输出结果如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "922e2759",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>names</th>\n",
       "      <th>scores</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>KNN</td>\n",
       "      <td>0.753247</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>SVC</td>\n",
       "      <td>0.805195</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>LR</td>\n",
       "      <td>0.766234</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>DT</td>\n",
       "      <td>0.766234</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>GNB</td>\n",
       "      <td>0.753247</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>RF</td>\n",
       "      <td>0.818182</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>GB</td>\n",
       "      <td>0.831169</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  names    scores\n",
       "0   KNN  0.753247\n",
       "1   SVC  0.805195\n",
       "2    LR  0.766234\n",
       "3    DT  0.766234\n",
       "4   GNB  0.753247\n",
       "5    RF  0.818182\n",
       "6    GB  0.831169"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for name,model in models:\n",
    "    model.fit(X_train,y_train)\n",
    "    #由考生填写\n",
    "    y_pred = model.predict(X_test)\n",
    "    #由考生填写\n",
    "    scores.append(accuracy_score(y_test,y_pred))\n",
    "    names.append(name)\n",
    "#由考生填写    \n",
    "tr_split = pd.DataFrame(data=list(zip(names,scores)),columns=['names','scores'])\n",
    "tr_split\n",
    "#由考生填写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "29becf0c",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import GridSearchCV\n",
    "# Specify parameters\n",
    "c_values = list(np.arange(1,10))\n",
    "param_gird = {'n_estimators':[5,50,250,500],'max_depth':[1,3,5,7,9],'learning_rate':[0.01,0.1,1,10,100]}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c0bbecc",
   "metadata": {},
   "source": [
    "3.使用 GridSearchCV 对 GradientBoostingClassifier 进行网格搜索，参数为 param_grid，使用5折交叉验证，评分标准为'accuracy。使用训练集(x_train，y_train)训练网格搜索模型，打印出最佳参数和最佳 estimator。并采用最佳 estimator 创建模型 new_model。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "49af5dee",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'learning_rate': 0.01, 'max_depth': 1, 'n_estimators': 500}\n",
      "GradientBoostingClassifier(learning_rate=0.01, max_depth=1, n_estimators=500)\n",
      "0.8051948051948052\n"
     ]
    }
   ],
   "source": [
    "#由考生填写\n",
    "from sklearn.metrics import make_scorer\n",
    "grid = GridSearchCV(estimator=GradientBoostingClassifier(),param_grid=param_gird,cv=5,scoring=make_scorer(accuracy_score))\n",
    "grid.fit(X_train,y_train)\n",
    "print(grid.best_params_)#打印出最佳参数\n",
    "print(grid.best_estimator_) # 打印最佳estimator\n",
    "new_model = grid.best_estimator_\n",
    "#由考生填写\n",
    "new_model.fit(X_train,y_train)\n",
    "y_pred = new_model.predict(X_test)\n",
    "print(accuracy_score(y_test,y_pred))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec39a3ac",
   "metadata": {},
   "source": [
    "4.初始化 StackingCVClassifer,要求至少定义3个基础分类器,LogisticRegression作为元分类器，使用3折交叉验证，要求StackingCVClassifer的得分accuracy_score至少大手 0.79。accuracy_score 打印输出为以下格式:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "f9901bff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "accuracy acore:0.7922077922077922\n"
     ]
    }
   ],
   "source": [
    "from mlxtend.classifier import StackingCVClassifier\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.svm import SVC\n",
    "#由考生填写\n",
    "dtc = DecisionTreeClassifier()\n",
    "lr = LogisticRegression()\n",
    "svc = SVC()\n",
    "sclf = StackingCVClassifier(classifiers=[dtc,lr,svc],meta_classifier=LogisticRegression(),cv=3)\n",
    "sclf.fit(X_train,y_train)\n",
    "y_pre = sclf.predict(X_test)\n",
    "#由考生填写\n",
    "print('accuracy acore:{}'.format(accuracy_score(y_test,y_pre)))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
